var baseurl="http://localhost/copy_QQ/public/";
function onMsgNotify(newMsgList) {
    alert("有新消息");
    console.log(newMsgList);
    var msgType=newMsgList[0].elems[0].type;//消息类型
    switch (msgType){
        case "TIMGroupTipElem"://群提示消息（只有群聊天才会出现）
            var optype=newMsgList[0].elems[0].content.opType;//操作类型
            switch (optype){
                case 1://进群
                    var account=newMsgList[0].elems[0].content.userIdList[0];
                    webim.getProfilePortrait({
                        "To_Account":[account],
                        "TagList":
                            [
                                "Tag_Profile_IM_Image",
                                "Tag_Profile_IM_Nick"
                            ]
                    },function (resp){
                        //console.log(resp);
                        var url=resp.UserProfileItem[0].ProfileItem[0].Value;
                        var nickname=resp.UserProfileItem[0].ProfileItem[1].Value;
                        var account=resp.UserProfileItem[0].To_Account;
                        addToGroup({
                            url:url,
                            nickname:nickname,
                            time:(new Date(newMsgList[0].time*1000)).toLocaleTimeString(),
                            account:account
                        });
                    },function (err) {
                        console.log(err.ErrorInfo);
                    });
                    break;
                case 2://退群

                    break;
                case 3://被踢出群

                    break;
                case 4://被设置成管理员

                    break;
                case 5://被取消管理员角色

                    break;
                case 6:

                    break;
                case 7://修改群资料

                    break;
                case 8://修改群成员信息

                    break;
                default:
                    break;
            }
            break;
        case "TIMTextElem"://文本消息
            webim.getProfilePortrait({
                "To_Account":[newMsgList[0].fromAccount],
                "TagList":
                    [
                        "Tag_Profile_IM_Image",
                        "Tag_Profile_IM_Nick"
                    ]
            },function (resp) {
                var item={
                    "fromAccountNick":"",
                    "url":"",
                    "content":"",
                    "time":"",
                    "fromAccount":""
                };
                var url=resp.UserProfileItem[0].ProfileItem[0].Value;
                item.url=url.replace(" ","%20");
                item.fromAccount=newMsgList[0].fromAccount;
                item.fromAccountNick=resp.UserProfileItem[0].ProfileItem[1].Value;;
                item.content=newMsgList[0].elems[0].content.text;
                item.time=(new Date(newMsgList[0].time*1000)).toLocaleTimeString();
                addToList(item);
            },function (err) {
                console.log(err.ErrorInfo);
            });
            break;
        case "MSG_ELEMENT_TYPE.FACE"://表情消息

            break;
        case "MSG_ELEMENT_TYPE.IMAGE"://图片消息

            break;
        case "MSG_ELEMENT_TYPE.SOUND"://语音消息

            break;
        case "MSG_ELEMENT_TYPE.FILE"://文件消息

            break;
        case "MSG_ELEMENT_TYPE.LOCATION"://位置消息

            break;
        case "MSG_ELEMENT_TYPE.CUSTOM"://自定义消息

            break;
        default:
            break;
    }


}

function onConnNotify(resp) {
    var info;
    switch (resp.ErrorCode) {
        case webim.CONNECTION_STATUS.ON:
            webim.Log.warn('建立连接成功: ' + resp.ErrorInfo);
            break;
        case webim.CONNECTION_STATUS.OFF:
            info = '连接已断开，无法收到新消息，请检查下您的网络是否正常: ' + resp.ErrorInfo;
            alert(info);
            webim.Log.warn(info);
            break;
        case webim.CONNECTION_STATUS.RECONNECT:
            info = '连接状态恢复正常: ' + resp.ErrorInfo;
            alert(info);
            webim.Log.warn(info);
            break;
        default:
            webim.Log.error('未知连接状态: =' + resp.ErrorInfo);
            break;
    }
}

function onBigGroupMsgNotify(res) {
    console.log(res);
}

function onGroupSystemNotifys(res) {
    //console.log(res);
    alert("有新成员加入");
    var options = {
        'GroupId': "@TGS#aQZSHNNFZ", //群 id
        'Applicant_Account': $("#hajg_to_account").val(), //申请人 id
        'HandleMsg': "Agree", //Agree-同意 Reject-拒绝
        'Authentication': $("#hajg_authentication").val(), //申请凭证
        'MsgKey': $("#hajg_msg_key").val(),
        'ApprovalMsg': $("#hajg_approval_msg").val(), //处理附言
        'UserDefinedField': ""//用户自定义字段
    };
}

function onGroupInfoChangeNotify(groupInfo) {
    console.log(groupInfo);

}

function onFriendSystemNotifys(res) {
    console.log(res);
}

function onProfileSystemNotifys(res) {
    console.log(res);
}

function onKickedEventCall(res) {
    console.log(res);
}

function onC2cEventNotifys(res) {
    console.log(res);
}

function loginIm(){
    //用户信息
    var loginInfo={
        sdkAppID:"1400139331",
        appIDAt3rd:"1400139331",
        identifier:sessionStorage.getItem("mobile"),
        identifierNick:sessionStorage.getItem("nickname"),
        userSig:sessionStorage.getItem("userSig"),
        accountType:"38182"
    };

    //监听事件
    var listeners = {
        "onConnNotify": onConnNotify,//监听连接状态回调变化事件,必填
        "onMsgNotify": onMsgNotify,//监听新消息(私聊，普通群(非直播聊天室)消息，全员推送消息)事件，必填
        "onBigGroupMsgNotify": onBigGroupMsgNotify,//监听新消息(直播聊天室)事件，直播场景下必填,"onGroupSystemNotifys": onGroupSystemNotifys//监听（多终端同步）群系统消息事件，如果不需要监听，可不填
        "onGroupInfoChangeNotify": onGroupInfoChangeNotify,//监听群资料变化事件，选填
        "onFriendSystemNotifys": onFriendSystemNotifys,//监听好友系统通知事件，选填,"onProfileSystemNotifys": onProfileSystemNotifys//监听资料系统（自己或好友）通知事件，选填
        "onKickedEventCall" : onKickedEventCall,//被其他登录实例踢下线
        "onC2cEventNotifys": onC2cEventNotifys,//监听 C2C 系统消息通道
    };
    webim.login(loginInfo,listeners,{},function (resp) {
            //console.log(resp);
            webim.setProfilePortrait({
                "ProfileItem":
                    [
                        {
                            "Tag":"Tag_Profile_IM_Nick",
                            "Value":sessionStorage.getItem("nickname")
                        },
                        {
                            "Tag":"Tag_Profile_IM_Image",
                            "Value":sessionStorage.getItem("headurl")
                        }
                    ]
            });

            //获取群成员详细信息
            $.ajax({
                url:baseurl+"api/tls_api/getGroupMemberInfo",
                type:"POST",
                data:{
                    "limit":100,
                    "offset":0,
                    "groupId":"@TGS#2JA5FZNFT"
                },
                success:function (res) {
                    //console.log(res);
                    for(let item of res.MemberList){
                        webim.getProfilePortrait({
                            "To_Account":[item.Member_Account],
                            "TagList":
                                [
                                    "Tag_Profile_IM_Image",
                                    "Tag_Profile_IM_Nick"
                                ]
                        },function (resp) {
                            //console.log(resp);
                            var url=resp.UserProfileItem[0].ProfileItem[0].Value;
                            var nickname=resp.UserProfileItem[0].ProfileItem[1].Value;
                            var account=resp.UserProfileItem[0].To_Account;
                            addToGroup({
                                url:url,
                                nickname:nickname,
                                time:(new Date(item.JoinTime*1000)).toLocaleTimeString(),
                                account:account
                            });
                        },function (err) {
                            console.log(err.ErrorInfo);
                        });

                    }
                }
            });

            //添加群成员
            $.ajax({
                url:baseurl+"api/tls_api/addGroupMember",
                type:"POST",
                data:{
                    "groupId":"@TGS#2JA5FZNFT",
                    "memberAccount":sessionStorage.getItem("mobile")
                },
                success:function (res) {
                    console.log(res);

                }
            });
        },
        function (err) {
            alert(err.ErrorInfo);
        });
}